
package AddressBook.View.Dialog;

import AddressBook.Model.Group;
import AddressBook.View.UtilityView.SpringUtilities;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * The Class AddItemDialog provides function to display and manage the add Contact dialog
 */
public class AddContactDialog extends JDialog {

    /**
     * This field keeps the reference of the firstNameTextField
     */
    private JTextField firstNameTextField;

    /**
     * This field keeps the reference of the firstNameTextField
     */
    private JTextField lastNameTextField;

    /**
     * This field keeps the reference of the groupComboBox
     */
    private JComboBox groupComboBox;

    private JTextField homeAdressTextField;
    private JTextField compagnyAdressTextField;
    private JTextField personalPhoneTextField;
    private JTextField professionalPhoneTextField;
    private JTextField homePageTextField;
    private JTextField emailTextField;
    private JTextField uriPhotoTextField;

    /**
     * This field keeps the reference of the addButton
     */
    private JButton addbutton;

    /**
     * This field keeps the reference of the cancelButton
     */
    private JButton cancelButton;

    /**
     * This field defines the number of char of the textfield
     */
    private static final int NUMBER_OF_CHAR = 50;

    public static final String URIPHOTO_FIELD_IS_EMPTY = "Uri Photo field is empty";
    public static final String EMAIL_FIELD_IS_EMPTY = "Email field is empty";
    public static final String HOMEPAGE_FIELD_IS_EMPTY = "HomePage field is empty";
    public static final String PROFESSIONALPHONE_FIELD_IS_EMPTY = "Professional Phone field is empty";
    public static final String PERSONALPHONE_FIELD_IS_EMPTY = "Personal Phone field is empty";
    public static final String COMPAGNYADDRESS_FIELD_IS_EMPTY = "Compagny Address field is empty";
    public static final String HOMEADDRESS_FIELD_IS_EMPTY = "Home Address field is empty";
    public static final String LASTNAME_FIELD_IS_EMPTY = "LastName field is empty";
    public static final String FIRSTNAME_FIELD_IS_EMPTY = "FirstName field is empty";


    /**
     * This constructor initializes the object with the corresponding parentView
     *
     * @param owner The parentView
     */
    public AddContactDialog(JFrame owner) {
        super(owner, owner.getTitle(), true);

        getContentPane().setLayout(new BorderLayout());

        Border panelBorder = BorderFactory.createEmptyBorder(20, 20, 20, 20);
        JPanel mainPanel = new JPanel(new SpringLayout());
        mainPanel.setBorder(panelBorder);

        /*
      This field store all the components
     */
        ArrayList<JComponent> componentArrayList = new ArrayList<JComponent>();

        firstNameTextField = new JTextField(NUMBER_OF_CHAR);
        componentArrayList.add(firstNameTextField);

        lastNameTextField = new JTextField(NUMBER_OF_CHAR);
        componentArrayList.add(lastNameTextField);

        groupComboBox = new JComboBox();
        componentArrayList.add(groupComboBox);

        homeAdressTextField = new JTextField(NUMBER_OF_CHAR);
        componentArrayList.add(homeAdressTextField);

        compagnyAdressTextField=new JTextField(NUMBER_OF_CHAR);
        componentArrayList.add(compagnyAdressTextField);

        personalPhoneTextField=new JTextField(NUMBER_OF_CHAR);
        componentArrayList.add(personalPhoneTextField);

        professionalPhoneTextField=new JTextField(NUMBER_OF_CHAR);
        componentArrayList.add(professionalPhoneTextField);

        homePageTextField=new JTextField(NUMBER_OF_CHAR);
        componentArrayList.add(homePageTextField);

        emailTextField=new JTextField(NUMBER_OF_CHAR);
        componentArrayList.add(emailTextField);

        uriPhotoTextField=new JTextField(NUMBER_OF_CHAR);
        componentArrayList.add(uriPhotoTextField);


        int i = 0;

        String[] labels = {"FirstName", "LastName", "Group", "Home Address", "Compagny Address", "Personal Phone","Professional Phone","HomePage","Email","UriPhoto"};
        for (JComponent component : componentArrayList) {
            JLabel l = new JLabel(labels[i++], JLabel.TRAILING);
            mainPanel.add(l);
            mainPanel.add(component);
        }

        SpringUtilities.makeCompactGrid(mainPanel,
                labels.length, 2, //rows, cols
                6, 6,        //initX, initY
                6, 6);       //xPad, yPad

        addbutton = new JButton("Add");
        cancelButton = new JButton("Cancel");

        add(mainPanel, BorderLayout.PAGE_START);
        add(addbutton, BorderLayout.LINE_END);
        add(cancelButton, BorderLayout.LINE_START);
        pack();
        setLocationRelativeTo(owner);
        setResizable(false);
    }

    /**
     * This method allows to attach an action listener to the add button
     *
     * @param l The desired actionListener
     */
    public void addActionListenerAddButton(ActionListener l) {
        addbutton.addActionListener(l);
    }

    /**
     * This method allows to attach an action listener to the cancel button
     *
     * @param l The desired actionListener
     */
    public void addActionListenerCancelButton(ActionListener l) {
        cancelButton.addActionListener(l);
    }

    /**
     * This method allows to set the family choice of the combobox
     *
     * @param obj An array of the families
     */
    public void setGroupField(Group[] obj) {
        groupComboBox.removeAllItems();
        for (Group p : obj) groupComboBox.addItem(p);
    }


    /**
     * This method return the family field
     *
     * @return The family field
     */
    public String getGroupField() {
        return ((Group) groupComboBox.getSelectedItem()).getName();
    }

    public String getFirstNameField() {
        return firstNameTextField.getText();
    }

    public String getLastNameField() {
        return lastNameTextField.getText();
    }

    public String getHomeAddressField() {
        return homeAdressTextField.getText();
    }

    public String getCompagnyAddessField() {
        return compagnyAdressTextField.getText();
    }

    public String getPersonalPhoneField() {
        return personalPhoneTextField.getText();
    }

    public String getProfessionalPhoneField() {
        return professionalPhoneTextField.getText();
    }

    public String getHomePageField() {
        return homePageTextField.getText();
    }

    public String getEmailField() {
        return emailTextField.getText();
    }

    public String getUriPhotoField() {
        return uriPhotoTextField.getText();
    }



    /**
     * This method reset all the fields
     */
    public void resetAllField() {
        firstNameTextField.setText("");
        lastNameTextField.setText("");
        homeAdressTextField.setText("");
        compagnyAdressTextField.setText("");
        personalPhoneTextField.setText("");
        professionalPhoneTextField.setText("");
        homePageTextField.setText("");
        emailTextField.setText("");
        uriPhotoTextField.setText("");
    }

}
